package linkedlist;


public class Solution {

    public ListNode removeElements(ListNode head,int val){


        //头节点就是给定的val的情况
        while (head != null && head.val == val){
           head = head.next;
        }

        //所有节点都等于给定val
        if(head == null){
            return null;
        }

        //中间的某一节点的值等于给定的val
        ListNode prev = head;
        while (prev.next != null){
            if(prev.next.val == val){
               prev.next = prev.next.next;
            }else {
                prev = prev.next;
            }
        }

        return head;

    }

}
